package se.webbzon.oschi01.action;

import java.awt.event.ActionEvent;

import se.webbzon.oschi01.command.Command;
import se.webbzon.oschi01.gui.ApplicationIconSheet;
import se.webbzon.oschi01.gui.ApplicationIcon;
import se.webbzon.oschi01.nbt.Tag;
import se.webbzon.oschi01.singleton.Singletons;

public class NBTDeleteAction extends DefaultAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2233569557157980808L;

	private static final ApplicationIconSheet iconSheet = Singletons.instance(ApplicationIconSheet.class);
	
	private final Tag<?> nbtTag;
	private final Tag<?> parent;
	
	public NBTDeleteAction(Tag<?> nbtTag) {
		this.nbtTag = nbtTag;
		parent = nbtTag.getParent();
		putValue(NAME, "Delete");
		putValue(SMALL_ICON, iconSheet.getIcon(ApplicationIcon.Delete));
	}
	
	@Override public void actionPerformed(ActionEvent e) {
		performCommand(new CommandImpl());
	}
	
	private final class CommandImpl implements Command {
		
		int index;

		@Override public String getName() {
			return "delete " + nbtTag;
		}

		@SuppressWarnings({ "unchecked", "rawtypes" })
		@Override public void redo() {
			Tag t = nbtTag;
			parent.getChildren().indexOf(t);
			index = parent.getChildren().indexOf(t);
			parent.getChildren().removeSibling(t);
		}

		@SuppressWarnings({ "rawtypes", "unchecked" })
		@Override public void undo() {
			Tag t = nbtTag;
			parent.getChildren().addSibling(t,index);
		}
		
	}

}
